package cn.iocoder.yudao.module.design.controller.admin.node;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.node.vo.*;
import cn.iocoder.yudao.module.design.convert.node.NodeConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.node.NodeDO;
import cn.iocoder.yudao.module.design.service.node.NodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 任务节点信息")
@RestController
@RequestMapping("/design/node")
@Validated
public class NodeController {

    @Resource
    private NodeService nodeService;

    @PostMapping("/create")
    @Operation(summary ="创建任务节点信息")
    @PreAuthorize("@ss.hasPermission('design:node:create')")
    public CommonResult<Long> createNode(@Valid @RequestBody NodeCreateReqVO createReqVO) {
        return success(nodeService.createNode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新任务节点信息")
    @PreAuthorize("@ss.hasPermission('design:node:update')")
    public CommonResult<Boolean> updateNode(@Valid @RequestBody NodeUpdateReqVO updateReqVO) {
        nodeService.updateNode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除任务节点信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:node:delete')")
    public CommonResult<Boolean> deleteNode(@RequestParam("id") Long id) {
        nodeService.deleteNode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得任务节点信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<NodeRespVO> getNode(@RequestParam("id") Long id) {
        NodeDO node = nodeService.getNode(id);
        return success(NodeConvert.INSTANCE.convert(node));
    }

    @GetMapping("/list")
    @Operation(summary ="获得任务节点信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<List<NodeRespVO>> getNodeList(@RequestParam("ids") Collection<Long> ids) {
        List<NodeDO> list = nodeService.getNodeList(ids);
        return success(NodeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得任务节点信息分页")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<PageResult<NodeRespVO>> getNodePage(@Valid NodePageReqVO pageVO) {
        PageResult<NodeDO> pageResult = nodeService.getNodePage(pageVO);
        return success(NodeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出任务节点信息 Excel")
    @PreAuthorize("@ss.hasPermission('design:node:export')")
   
    public void exportNodeExcel(@Valid NodeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<NodeDO> list = nodeService.getNodeList(exportReqVO);
        // 导出 Excel
        List<NodeExcelVO> datas = NodeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "任务节点信息.xls", "数据", NodeExcelVO.class, datas);
    }

    @GetMapping("/all")
    @Operation(summary ="获得流程信息分页")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<List<NodeListVo>> getNodeAll(@Valid NodeBaseVO baseVO) {
        List<NodeListVo> list = nodeService.getNodeList(baseVO);
        return success(list);
    }


    @GetMapping("/getToDoTaskList")
    @Operation(summary ="getToDoTaskList")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<List<NodeDO>> getToDoTaskList(@Valid NodeBaseVO baseVO) {
        // 进行中
        baseVO.setStatusCd("1");

        List<NodeDO> list = nodeService.getToDoTaskList(baseVO);
        return success(list);
    }


    @PostMapping("/createPurchaseNode")
    @Operation(summary ="批量创建采购流程")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<Boolean> createPurchaseNode(@Valid @RequestBody NodePurchaseVO baseVO) {


         nodeService.createPurchaseNode(baseVO);
        return success(true);
    }

    @GetMapping("/openNodeTask")
    @Operation(summary ="获得任务节点信息")
    @Parameter(name = "id", description = "节点编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<Boolean> openNodeTask(@RequestParam("id") Long id) {
   nodeService.openNodeTaskByNodeId(id);
        return success(true);
    }

    @PostMapping("/createConstructionProcess")
    @Operation(summary ="新建施工流程")
    @Parameter(name = "id", description = "节点编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:node:query')")
    public CommonResult<Boolean> createConstructionProcess(@Valid @RequestBody NodeConstrucProcessVo baseVO) {

        nodeService.createConstructionProcess(baseVO);
        return success(true);
    }


}
